﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using PS.DBUtility;
namespace PS.DAL
{
    /// <summary>
    /// 数据访问类:RepProjectResource
    /// </summary>
    public partial class RepProjectResource
    {
        public RepProjectResource()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int DataID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from RepProjectResource");
            strSql.Append(" where DataID=@DataID");
            SqlParameter[] parameters = {
					new SqlParameter("@DataID", SqlDbType.Int,4)
};
            parameters[0].Value = DataID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(PS.Model.RepProjectResource model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into RepProjectResource(");
            strSql.Append("RepProjectInfoID,DatumTitle,FileType,DateTypeID,FileName,KeyWord,DataMemo,Creator,CreateTime,Editor,EditTime,DispOrder,ValidMark,Spare1,Spare2,Spare3,Spare4)");
            strSql.Append(" values (");
            strSql.Append("@RepProjectInfoID,@DatumTitle,@FileType,@DateTypeID,@FileName,@KeyWord,@DataMemo,@Creator,@CreateTime,@Editor,@EditTime,@DispOrder,@ValidMark,@Spare1,@Spare2,@Spare3,@Spare4)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@RepProjectInfoID", SqlDbType.Int,4),
					new SqlParameter("@DatumTitle", SqlDbType.VarChar,100),
					new SqlParameter("@FileType", SqlDbType.VarChar,20),
					new SqlParameter("@DateTypeID", SqlDbType.Int,4),
					new SqlParameter("@FileName", SqlDbType.VarChar,200),
					new SqlParameter("@KeyWord", SqlDbType.VarChar,200),
					new SqlParameter("@DataMemo", SqlDbType.VarChar,200),
					new SqlParameter("@Creator", SqlDbType.VarChar,20),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@Editor", SqlDbType.VarChar,20),
					new SqlParameter("@EditTime", SqlDbType.DateTime),
					new SqlParameter("@DispOrder", SqlDbType.Int,4),
					new SqlParameter("@ValidMark", SqlDbType.Int,4),
					new SqlParameter("@Spare1", SqlDbType.VarChar,200),
					new SqlParameter("@Spare2", SqlDbType.VarChar,200),
					new SqlParameter("@Spare3", SqlDbType.VarChar,200),
					new SqlParameter("@Spare4", SqlDbType.VarChar,200)};
            parameters[0].Value = model.RepProjectInfoID;
            parameters[1].Value = model.DatumTitle;
            parameters[2].Value = model.FileType;
            parameters[3].Value = model.DateTypeID;
            parameters[4].Value = model.FileName;
            parameters[5].Value = model.KeyWord;
            parameters[6].Value = model.DataMemo;
            parameters[7].Value = model.Creator;
            parameters[8].Value = model.CreateTime;
            parameters[9].Value = model.Editor;
            parameters[10].Value = model.EditTime;
            parameters[11].Value = model.DispOrder;
            parameters[12].Value = model.ValidMark;
            parameters[13].Value = model.Spare1;
            parameters[14].Value = model.Spare2;
            parameters[15].Value = model.Spare3;
            parameters[16].Value = model.Spare4;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(PS.Model.RepProjectResource model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update RepProjectResource set ");
            strSql.Append("RepProjectInfoID=@RepProjectInfoID,");
            strSql.Append("DatumTitle=@DatumTitle,");
            strSql.Append("FileType=@FileType,");
            strSql.Append("DateTypeID=@DateTypeID,");
            strSql.Append("FileName=@FileName,");
            strSql.Append("KeyWord=@KeyWord,");
            strSql.Append("DataMemo=@DataMemo,");
            strSql.Append("Creator=@Creator,");
            strSql.Append("CreateTime=@CreateTime,");
            strSql.Append("Editor=@Editor,");
            strSql.Append("EditTime=@EditTime,");
            strSql.Append("DispOrder=@DispOrder,");
            strSql.Append("ValidMark=@ValidMark,");
            strSql.Append("Spare1=@Spare1,");
            strSql.Append("Spare2=@Spare2,");
            strSql.Append("Spare3=@Spare3,");
            strSql.Append("Spare4=@Spare4");
            strSql.Append(" where DataID=@DataID");
            SqlParameter[] parameters = {
					new SqlParameter("@RepProjectInfoID", SqlDbType.Int,4),
					new SqlParameter("@DatumTitle", SqlDbType.VarChar,100),
					new SqlParameter("@FileType", SqlDbType.VarChar,20),
					new SqlParameter("@DateTypeID", SqlDbType.Int,4),
					new SqlParameter("@FileName", SqlDbType.VarChar,200),
					new SqlParameter("@KeyWord", SqlDbType.VarChar,200),
					new SqlParameter("@DataMemo", SqlDbType.VarChar,200),
					new SqlParameter("@Creator", SqlDbType.VarChar,20),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@Editor", SqlDbType.VarChar,20),
					new SqlParameter("@EditTime", SqlDbType.DateTime),
					new SqlParameter("@DispOrder", SqlDbType.Int,4),
					new SqlParameter("@ValidMark", SqlDbType.Int,4),
					new SqlParameter("@Spare1", SqlDbType.VarChar,200),
					new SqlParameter("@Spare2", SqlDbType.VarChar,200),
					new SqlParameter("@Spare3", SqlDbType.VarChar,200),
					new SqlParameter("@Spare4", SqlDbType.VarChar,200),
					new SqlParameter("@DataID", SqlDbType.Int,4)};
            parameters[0].Value = model.RepProjectInfoID;
            parameters[1].Value = model.DatumTitle;
            parameters[2].Value = model.FileType;
            parameters[3].Value = model.DateTypeID;
            parameters[4].Value = model.FileName;
            parameters[5].Value = model.KeyWord;
            parameters[6].Value = model.DataMemo;
            parameters[7].Value = model.Creator;
            parameters[8].Value = model.CreateTime;
            parameters[9].Value = model.Editor;
            parameters[10].Value = model.EditTime;
            parameters[11].Value = model.DispOrder;
            parameters[12].Value = model.ValidMark;
            parameters[13].Value = model.Spare1;
            parameters[14].Value = model.Spare2;
            parameters[15].Value = model.Spare3;
            parameters[16].Value = model.Spare4;
            parameters[17].Value = model.DataID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int DataID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from RepProjectResource ");
            strSql.Append(" where DataID=@DataID");
            SqlParameter[] parameters = {
					new SqlParameter("@DataID", SqlDbType.Int,4)
};
            parameters[0].Value = DataID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string DataIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from RepProjectResource ");
            strSql.Append(" where DataID in (" + DataIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public PS.Model.RepProjectResource GetModel(int DataID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 DataID,RepProjectInfoID,DatumTitle,FileType,DateTypeID,FileName,KeyWord,DataMemo,Creator,CreateTime,Editor,EditTime,DispOrder,ValidMark,Spare1,Spare2,Spare3,Spare4 from RepProjectResource ");
            strSql.Append(" where DataID=@DataID");
            SqlParameter[] parameters = {
					new SqlParameter("@DataID", SqlDbType.Int,4)
};
            parameters[0].Value = DataID;

            PS.Model.RepProjectResource model = new PS.Model.RepProjectResource();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["DataID"].ToString() != "")
                {
                    model.DataID = int.Parse(ds.Tables[0].Rows[0]["DataID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RepProjectInfoID"].ToString() != "")
                {
                    model.RepProjectInfoID = int.Parse(ds.Tables[0].Rows[0]["RepProjectInfoID"].ToString());
                }
                model.DatumTitle = ds.Tables[0].Rows[0]["DatumTitle"].ToString();
                model.FileType = ds.Tables[0].Rows[0]["FileType"].ToString();
                if (ds.Tables[0].Rows[0]["DateTypeID"].ToString() != "")
                {
                    model.DateTypeID = int.Parse(ds.Tables[0].Rows[0]["DateTypeID"].ToString());
                }
                model.FileName = ds.Tables[0].Rows[0]["FileName"].ToString();
                model.KeyWord = ds.Tables[0].Rows[0]["KeyWord"].ToString();
                model.DataMemo = ds.Tables[0].Rows[0]["DataMemo"].ToString();
                model.Creator = ds.Tables[0].Rows[0]["Creator"].ToString();
                if (ds.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    model.CreateTime = DateTime.Parse(ds.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                model.Editor = ds.Tables[0].Rows[0]["Editor"].ToString();
                if (ds.Tables[0].Rows[0]["EditTime"].ToString() != "")
                {
                    model.EditTime = DateTime.Parse(ds.Tables[0].Rows[0]["EditTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DispOrder"].ToString() != "")
                {
                    model.DispOrder = int.Parse(ds.Tables[0].Rows[0]["DispOrder"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ValidMark"].ToString() != "")
                {
                    model.ValidMark = int.Parse(ds.Tables[0].Rows[0]["ValidMark"].ToString());
                }
                model.Spare1 = ds.Tables[0].Rows[0]["Spare1"].ToString();
                model.Spare2 = ds.Tables[0].Rows[0]["Spare2"].ToString();
                model.Spare3 = ds.Tables[0].Rows[0]["Spare3"].ToString();
                model.Spare4 = ds.Tables[0].Rows[0]["Spare4"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DataID,a.RepProjectInfoID,c.ProjectName,a.DatumTitle,a.FileType,a.FileName,d.KeyValue as FileTypeName,'<a href=''/Upload/'+a.FileName+'''  target=_blank>'+a.DatumTitle+'</a>' as FileNameUrl,a.DateTypeID,b.KeyValue  as DateTypeName,KeyWord,DataMemo,a.Creator,a.CreateTime,a.Editor,a.EditTime,a.DispOrder,a.ValidMark,a.Spare1,a.Spare2,a.Spare3,a.Spare4,c.ConstructDepID,c.ProjectPrincipal from RepProjectResource a left join SysDictionarySub b on a.DateTypeID=b.ID and b.MainKeyID=15 and a.ValidMark=1 left join RepProjectInfo c on a.RepProjectInfoID=c.RepProjectInfoID left join SysDictionarySub d on a.FileType= d.ID and d.MainKeyID=4 ");
            //strSql.Append(" FROM RepProjectResource ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 分页查询公司信息
        /// </summary>
        /// <param name="compName">公司名称，模糊查询</param>
        /// <param name="pageSize">每页行数</param>
        /// <param name="pageIndex">第几页</param>
        /// <param name="totalCount">返回总行数</param>
        /// <returns></returns>
        public DataSet GetList(string strWhere, long pageSize, long pageIndex, out long totalCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DataID,a.RepProjectInfoID,c.ProjectName,a.DatumTitle,a.FileType,a.FileName,d.KeyValue as FileTypeName,'<a href=''/Upload/'+a.FileName+''' target=_blank>'+a.DatumTitle+'</a>' as FileNameUrl,a.DateTypeID,b.KeyValue  as DateTypeName,KeyWord,DataMemo,a.Creator,a.CreateTime,a.Editor,a.EditTime,a.DispOrder,a.ValidMark,a.Spare1,a.Spare2,a.Spare3,a.Spare4,c.ConstructDepID,c.ProjectPrincipal from RepProjectResource a left join SysDictionarySub b on a.DateTypeID=b.ID and b.MainKeyID=15 and a.ValidMark=1  left join RepProjectInfo c on a.RepProjectInfoID=c.RepProjectInfoID left join  SysDictionarySub d on a.FileType= d.ID and d.MainKeyID=4  ");
            //strSql.Append(" FROM RepProjectResource ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append("  order by a.CreateTime desc");

            string totalSql = PageSplitHelper.GetTotalSql(strSql.ToString());
            totalCount = long.Parse(DbHelperSQL.ExecuteScalar(totalSql).ToString());

            string pageSql = PageSplitHelper.GetPageSql(strSql.ToString(), pageSize, pageIndex, totalCount);

            return DbHelperSQL.Query(pageSql);
        }


        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" DataID,RepProjectInfoID,DatumTitle,FileType,DateTypeID,FileName,KeyWord,DataMemo,Creator,CreateTime,Editor,EditTime,DispOrder,ValidMark,Spare1,Spare2,Spare3,Spare4 ");
            strSql.Append(" FROM RepProjectResource ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "RepProjectResource";
            parameters[1].Value = "DataID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

